<?php
namespace Home\Controller;
use Think\Controller;
class ClubadminController extends Controller{
public function clubadmin(){
      //调用模型层操作数据
           $clubadminmodel=M('clubadmin');
           //var_dump($goodsmodel);
            $this->clubadmin=$clubadminmodel->order('club_adminid desc')->select();
      //调用视图层显示结果
           $this->display();
    }


public function add(){
    // var_dump($_FILES);exit;
    $clubadmin = D('clubadmin');
    $clubadmin->club_adminname=$_POST['name'];
    $clubadmin->password=$_POST['password'];
    $clubadmin->belongsclub=$_POST['belongsclub'];
      if($_FILES["clubadminpic"]["error"]==0){
    $info=$this->upload();
    // var_dump($info);exit;
      $clubadmin->clubadminpic=$info["clubadminpic"]["savename"];
  }
    else{
      $this->error("必须删除图片"); 
    }
  $clubadmin->password=md5(I('post.password')); //对密码进行Md5加密
  if (false !== $clubadmin->add()) {
                $this->success("注册成功!","Clubadmin" );
            } else {         $this->error('数据错误');           }
          

     }
     public function upload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/Uploads/clubadmin/'; // 设置附件上传根目录
    $upload->autoSub=false;//关闭子目录，默认ture
    //$upload->savePath  =     ''; // 设置附件上传（子）目录,默认目录名为日期
    $info   =   $upload->upload();  // 上传文件 
    if(!$info) { 
    $this->error($upload->getError());    }
    else{
    return $info;   }                    
  }
     public function updataClubadmin(){
        $id=$_POST['club_adminid'];
     
        $clubadmin = M('clubadmin');
        $pic=$clubadmin->where("club_adminid=".$id)->find();
        if($clubadmin->create()){
            if ($_FILES['clubadminpic']['error'] == 0) {
                $info = $this->upload();
                // var_dump("id");
                $name=$info['clubadminpic']['savename'];
                unlink(realpath("./Public/Uploads/clubadmin/".$pic["clubadminpic"]));
            } else {
              
                $name = $pic["clubadminpic"];
            }
            $clubadmin->clubadminpic=$name;
            if (false !== $clubadmin->where("club_adminid=".$id)->save()) {
                $this->success('修改成功！', U('clubadmin'));
            } else {         
                $this->error('数据错误','');           
            }
        }else {            
            $this->error($clubadmin->getError());  
        }
    }

      public function delete(){

        $id=I('get.club_adminid');
        $clubadminModel=M("clubadmin");
        $pic = $clubadminModel->where("club_adminid=".$id)->field("clubadminpic")->find();
        $res=$clubadminModel->delete($id);
        unlink(realpath("./Public/Uploads/clubadmin/".$pic["clubadminpic"]));
        if($res!=false){$this->success("删除成功");}
        else{$this->error("删除失败");}
    }



 }?>